﻿//@Author: http://www.aspsnippets.com/
//
//

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Threading;
using System.Net.Mail;
using System.Net;
using System.IO;

namespace AgriMarketService
{
    public class EmailSender
    {
        public static void SendAsyncEmail(string to, string from, string password, string subject, string body/*, HttpPostedFile postedFile*/)
        {
            Thread email = new Thread(delegate()
            {
                SendEmail(to, from, password, subject, body/*, postedFile*/);
            });

            email.IsBackground = true;
            email.Start();
        }

        public static void SendEmail(string to, string from, string password, string subject, string body/*, HttpPostedFile postedFile*/)
        {
            using (MailMessage mm = new MailMessage(from, to))
            {
                mm.Subject = subject;
                mm.Body = body;
                //if (postedFile.ContentLength > 0)
                //{
                //    string fileName = Path.GetFileName(postedFile.FileName);
                //    mm.Attachments.Add(new Attachment(postedFile.InputStream, fileName));
                //}
                mm.IsBodyHtml = false;
                SmtpClient smtp = new SmtpClient();
                smtp.Host = "smtp.gmail.com";
                smtp.EnableSsl = true;
                NetworkCredential NetworkCred = new NetworkCredential(from, password);
                smtp.UseDefaultCredentials = true;
                smtp.Credentials = NetworkCred;
                smtp.Port = 587;
                smtp.Send(mm);
            }
        }
    }
}